<script>
	import {useUserData} from '@/hooks/useUserData';
	import {useStore} from 'vuex';
	import {useToken} from './hooks/useToken';
	import interceptor from '@/router/router.interceptor';

	export default {
		onLaunch: function() {
			console.log('App Launch')
			this.loginByToken()
		},
		onShow: function() {
			console.log('App Show')
			this.forbidFontChange();
			setTimeout(() => {
				interceptor.beforeEach({
					url: '/' + getCurrentPages()[getCurrentPages().length - 1].route
				});
			}, 1000)
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			async loginByToken() {
				const store = useStore();
				const userId = store.state.userInfo?store.state.userInfo.id:0;
				const {
					getTokenSync
				} = useToken();
				if (userId && getTokenSync()) {
					const {
						getUserInfo
					} = useUserData();
					try {
						const userInfo = await getUserInfo(userId);
						uni.store.local('userInfo', userInfo);
						uni.loginManger.$resolve();
					} catch {
						this.logOut();
					}
				}
			},
			logOut() {
				const {
					removeTokenSync
				} = useToken();
				uni.router.route('/pages_login/login_entry/loginEntry');
			},
			forbidFontChange() {
				if (typeof WeixinJSBridge == "object" && typeof WeixinJSBridge.invoke == "function") {
					handleFontSize();
				} else {
					document.addEventListener("WeixinJSBridgeReady", handleFontSize, false);
				}

				function handleFontSize() {
					console.log('handleFontSize')
					// 设置网页字体为默认大小
					WeixinJSBridge.invoke('setFontSizeCallback', {
						'fontSize': 0
					});
					// 重写设置网页字体大小的事件
					WeixinJSBridge.on('menu:setfont', function() {
						WeixinJSBridge.invoke('setFontSizeCallback', {
							'fontSize': 0
						});
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	// @import "@/static/style/global.scss";
	@import "@/uni.scss";
	@import "@/static/iconfont/iconfont.css";
</style>